package com.gesee.zyspps;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;

import com.gesee.zyspps.db.DaoMaster;
import com.gesee.zyspps.db.DaoMaster.OpenHelper;
import com.gesee.zyspps.db.DaoSession;
import com.gesee.zyspps.utils.Constants;
import com.gesee.zyspps.utils.LogUtil;

public class ZSApplication extends Application
{

    private Object nowPageContext = null;
    private Activity nowActivity = null;
    private static ZSApplication instance;
    private static Context sContext;
    public final static String CONFIG = "config";
    private ArrayList<Activity> activityList = new ArrayList<Activity>();

    @Override
    public void onCreate()
    {
        super.onCreate();
        initContext();
        instance = this;
        // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
//        SDKInitializer.initialize(this);
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());
    }

    
    public static Context getContext() {
        return sContext;
    }

    private void initContext() {
        sContext = getApplicationContext();
    }
    
    /**
     * 
     * @Title: setOnActivityCreate
     * @Description: TODO(activity 创建)
     * @author fujianxiong
     * @param activity
     * @throws
     */
    public void setOnActivityCreate(Activity activity)
    {
        if (null == activityList)
        {
            activityList = new ArrayList<Activity>();
        }
        activityList.add(0, activity);
        setCurrentActivity(activity);
        setNowPageContext(activity);
    }

    /**
     * 
     * @Title: setOnActivityDestroy
     * @Description: TODO(activity 销毁)
     * @author fujianxiong
     * @param activity
     * @throws
     */
    public void setOnActivityDestroy(Activity activity)
    {
        if (null != activityList && activityList.size() > 0)
        {
            activityList.remove(activity);
            setCurrentActivity();
        }
    }

    /**
     * activity Resume
     * 
     * @param activity
     */
    public void setOnActivityResume(Activity activity)
    {
        setCurrentActivity(activity);
        setNowPageContext(activity);

    }

    /**
     * fragment Resume
     * 
     * @param fragment
     */
    public void setOnFragmentResume(Fragment fragment)
    {
        setNowPageContext(fragment);
    }

    /**
     * 
     * @Title: setCurrentActivity
     * @Description: TODO(设置当前的activity)
     * @author fujianxiong
     * @throws
     */
    private void setCurrentActivity()
    {
        if (null != activityList && activityList.size() > 0)
        {
            nowActivity = activityList.get(0);
        }
    }

    /**
     * 
     * @Title: setCurrentActivity
     * @Description: TODO(设置当前的activity)
     * @author fujianxiong
     * @param activity
     * @throws
     */
    private void setCurrentActivity(Activity activity)
    {
        nowActivity = activity;
    }

    public void finishAllActivity()
    {
        int size = activityList.size();
        for (int i = 0; i < size; i++)
        {
            Activity activity = getCurrentActivity();
            activity.finish();
            ZSApplication.getInstance().setOnActivityDestroy(activity);
        }
        activityList.clear();
    }

    public void finishActivity()
    {
        finishActivity(1);
    }

    public void finishActivity(int count)
    {
        if (count > getActivitysCount())
        {
            LogUtil.e("finishActivity params count is error");
            return;
        }
        for (int i = 0; i < count; i++)
        {
            Activity activity = activityList.get(i);
            LogUtil.i("finishActivity is : 【" + activity.getComponentName() + "】");
            activity.finish();
            ZSApplication.getInstance().setOnActivityDestroy(activity);
        }
    }

    public void finishActivity(List<Class<?>> activityClassList)
    {
        List<Activity> templist = new ArrayList<Activity>();
        for (Class<?> activityClass : activityClassList)
        {
            for (Activity mActivity : activityList)
            {
                if (mActivity.getClass().equals(activityClass))
                {
                    templist.add(mActivity);
                }
            }
        }

        for (Activity tempActivity : templist)
        {
            activityList.remove(tempActivity);
            tempActivity.finish();
        }
    }

    /**
     * 
     * @Title: finishActivity
     * @Description: TODO(结束指定Activity)
     * @author fujianxiong
     * @param activity
     * @throws
     */
    public void finishActivity(Activity activity)
    {
        if (activity != null && activityList.contains(activity))
        {
            activityList.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 
     * @Title: finishActivity
     * @Description: TODO(结束指定类名的Activity)
     * @author fujianxiong
     * @param cls
     * @throws
     */
    public void finishActivity(Class<?> cls)
    {
        for (Activity activity : activityList)
        {
            if (activity.getClass().equals(cls))
            {
                activityList.remove(activity);
            }
        }
    }

    /**
     * 
     * @Title: getCurrentActivity
     * @Description: TODO(获取当前activity)
     * @author fujianxiong
     * @return
     * @throws
     */
    public Activity getCurrentActivity()
    {
        int size = activityList.size();
        LogUtil.d("currentActivity is : 【" + nowActivity.getComponentName() + "】  activity count : 【" + size + "】");
        for (int i = 0; i < size; i++)
        {
            LogUtil.d("activity stack position " + i + ": activity = " + activityList.get(i).getComponentName());
        }
        LogUtil.d("currentActivity: " + nowActivity.getComponentName() + " activity in the list of position: 【" + activityList.indexOf(this.nowActivity) + "】");
        return this.nowActivity;
    }

    /**
     * 
     * @Title: getPreActivity
     * @Description: TODO(获取上一个activity)
     * @author fujianxiong
     * @return
     * @throws
     */
    public Activity getPreActivity()
    {
        if (activityList.size() == 1)
        {
            return null;
        }
        return activityList.get(activityList.size() - 2);
    }

    /**
     * 
     * @Title: getActivity
     * @Description: TODO(根据activityName获取activity)
     * @author fujianxiong
     * @param mActivity
     * @return
     * @throws
     */
    @SuppressWarnings("unchecked")
    public Class<? extends Activity> getActivity(Class<? extends Activity> mActivity)
    {
        if (activityList.size() == 0)
        {
            return mActivity;
        }
        if (activityList.contains(mActivity))
        {
            for (Activity activityItem : activityList)
            {
                if (activityItem.getComponentName().equals(mActivity.getName()))
                {
                    mActivity = (Class<Activity>) activityItem.getClass();
                    break;
                }
            }
        }
        return mActivity;
    }

    /**
     * 
     * @Title: getActivitysCount
     * @Description: TODO(返回 activity 总数)
     * @author fujianxiong
     * @return
     * @throws
     */
    public int getActivitysCount()
    {
        return activityList.size();
    }

    public Object getNowPageContext()
    {
        return nowPageContext;
    }

    public void setNowPageContext(Object nowPageContext)
    {
        this.nowPageContext = nowPageContext;
    }

    public static ZSApplication getInstance()
    {
        return instance;
    }

    /**
     * 取得DaoMaster
     * 
     * @param context
     * @return
     */
    public static DaoMaster getDaoMaster(String dbName)
    {
        DaoMaster daoMaster = null;
        if (daoMaster == null)
        {
            OpenHelper helper = new DaoMaster.DevOpenHelper(instance, dbName, null);
            daoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        return daoMaster;
    }

    public static DaoMaster getDaoMaster()
    {
        DaoMaster zsDaoMaster = null;
        if (zsDaoMaster == null)
        {
            OpenHelper helper = new DaoMaster.DevOpenHelper(instance, Constants.DB_NAME, null);
            zsDaoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        return zsDaoMaster;
    }

    /**
     * 取得DaoSession
     * 
     * @param context
     * @return
     */
    public static DaoSession getDaoSession(String dbName)
    {
        DaoSession daoSession =null;
        DaoMaster daoMaster = null;
        if (daoSession == null)
        {
            if (daoMaster == null)
            {
                daoMaster = getDaoMaster(dbName);
            }
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }

    public static DaoSession getDaoSession()
    {
        DaoSession zsDaoSession = null;
        DaoMaster zsDaoMaster = null;
        if (zsDaoSession == null)
        {
            if (zsDaoMaster == null)
            {
                zsDaoMaster = getDaoMaster();
            }
            zsDaoSession = zsDaoMaster.newSession();
        }
        return zsDaoSession;
    }

    /**
     * 
     * @Title: setConfig
     * @Description: TODO(配置参数的key.配置参数的值)
     * @author fujianxiong
     * @param key
     * @param value
     * @throws
     */
    public void setConfig(String key, String value)
    {
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(CONFIG, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        LogUtil.d("editConfig : key = " + key + " , value = " + value);
        editor.commit();
    }

    /**
     * 
     * @Title: getConfig
     * @Description: TODO(配置参数的key)
     * @author fujianxiong
     * @param key
     * @return
     * @throws
     */
    public String getConfig(String key)
    {
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(CONFIG, MODE_PRIVATE);
        String orderId = sharedPreferences.getString(key, "null");
        LogUtil.d("readConfig : key = " + key + " , value = " + sharedPreferences.getString(key, ""));
        return orderId;
    }
    
    public String getConfig1(String key)
    {
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(CONFIG, MODE_PRIVATE);
        String orderId = sharedPreferences.getString(key, "");
        LogUtil.d("readConfig : key = " + key + " , value = " + sharedPreferences.getString(key, ""));
        return orderId;
    }

}